
$(function () {
    //计算器
    $('#calculaterBtn').click(function () {
        // $('#calculator').isValid(function(v){
        // if(true){
        var money = $('#cal_money').val();//贷款金额(万元)
        var allM = money * 10000;//贷款金额(元)
        var month = $('#cal_month').val(); // 期限
        var rate = $('#cal_rate').val();// 月利率（％）
        var yp = rate * 12 / 100;// 年利率
        var mp = rate / 100;// 月利率

        //计算贷款
        var calType = $('#cal_type').val();
        var data;
        //  var results=[]; 
        var results;
        if (calType == '1') {
            //等额本息
            data = calculator.benxi(allM, yp, month);
            var mIncome = (allM * mp * Math.pow(1 + mp, month)) / (Math.pow(1 + mp, month) - 1);
            var monthBM = 0;
            var mi = 0;
            var money = allM;
            var tmpMoney = 0;
            var allP = 0;
            for (var i = 1; i <= month; i++) {
                monthBM = (allM * mp * (Math.pow((1 + mp), i - 1))) / (Math.pow(1 + mp, month) - 1);
                mi = money * mp;
                money = money - monthBM;
                allP = allP + mi;
                var html = '<tr>';
                html += '<td>' + i + '</td>';
                html += '<td>' + mIncome.toFixed(2) + '</td>';
                html += '<td>' + mi.toFixed(2) + '</td>';
                html += '<td>' + monthBM.toFixed(2) + '</td>';
                html += '<td>' + money.toFixed(2) + '</td>';
                html += '</tr>';
                // results[i-1]=html;  
                results += html;
            }
        } else if (calType == '2') {
            //等额本金
            data = calculator.benjin(allM, yp, month);
            var mpm = 0;
            var tmpMoney = 0;
            var mi = 0;
            var rm = allM;
            var allP = 0;
            for (var i = 1; i <= month; i++) {
                mpm = (allM / month) + (allM - tmpMoney) * mp;
                mi = (allM - tmpMoney) * mp;
                tmpMoney = tmpMoney + (allM / month);
                var baseM = (allM / month);
                rm = (rm - baseM);
                //callback_m(i, mpm.toFixed(2), baseM.toFixed(2), mi.toFixed(2), rm.toFixed(2));
                var html = '<tr>';
                html += '<td>' + i + '</td>';
                html += '<td>' + mpm.toFixed(2) + '</td>';
                html += '<td>' + mi.toFixed(2) + '</td>';
                html += '<td>' + baseM.toFixed(2) + '</td>';
                html += '<td>' + rm.toFixed(2) + '</td>';
                html += '</tr>';
                //results[i-1]=html;  
                results += html;
            }
        } else if (calType == '3') {
            //先息后本
            data = calculator.daoqhb(allM, yp, month);
            var lixiTotal = (((allM * yp) / 12) * month).toFixed(2);//利息总和  allPay
            var benxiTotal = Number(allM) + Number(lixiTotal);//本息合计 all
            for (var i = 1; i <= month; i++) {
                var repayMonth = (Number(lixiTotal) / Number(month)).toFixed(2);//每月还款 monthAll 
                var html = '<tr>';
                html += '<td>' + i + '</td>';
                html += '<td>' + repayMonth + '</td>';
                html += '<td>' + repayMonth + '</td>';
                html += '<td>' + 0 + '</td>';
                html += '<td>' + 0 + '</td>';
                html += '</tr>';
                //results[i-1]=html; 
                results += html;
            }
        } else if (calType == '4') {
            //一次还本付息
            data = calculator.daoqhb(allM, yp, month);
            data.monthAll = 0;
            var lixiTotal = (((allM * yp) / 12) * month).toFixed(2);//利息总和  allPay
            var benxiTotal = Number(allM) + Number(lixiTotal);//本息合计 all
            for (var i = 1; i <= month; i++) {
                var repayMonth = (Number(lixiTotal) / Number(month)).toFixed(2);//每月还款 monthAll 
                var html = '<tr>';
                html += '<td>' + i + '</td>';
                html += '<td>' + repayMonth + '</td>';
                html += '<td>' + repayMonth + '</td>';
                html += '<td>' + 0 + '</td>';
                html += '<td>' + 0 + '</td>';
                html += '</tr>';
                //results[i-1]=html; 
                results += html;
            }
        }
        $('#cal_month_money').text(Number(data.monthAll));//每月还款
        $('#cal_interest_money').text(Number(data.allPay));//总利息
        $('#cal_all_money').text(data.all);//本息合计
        $('#dout tbody').html('');
        $('#dout tbody').append(results);
        // }
        return false;
    });


    var calculator = {
		/**
		 * 等额本息，计算总还款利息
		 * @param bMoney 贷款金额
		 * @param yearRatio 年利率%，如年利率5.6%就为5.6
		 * @param month 贷款月数
		 * @return mixed
		 */

        benxi: function (bMoney, yearRatio, month, callback_m) {
            var yp = yearRatio; //年利率
            var mp = yearRatio / 12; // 月利率
            //var month = t * 12; // 期限
            var allM = bMoney;
            //================================//
            // 每月本息金额  = (本金×月利率×(1＋月利率)＾还款月数)÷ ((1＋月利率)＾还款月数-1)
            var mIncome = (allM * mp * Math.pow(1 + mp, month)) / (Math.pow(1 + mp, month) - 1);
            //================================//
            // 每月本金
            var monthBM = 0;
            var mi = 0;
            var money = allM;
            var tmpMoney = 0;
            var allP = 0;
            for (var i = 1; i <= month; i++) {
                monthBM = (allM * mp * (Math.pow((1 + mp), i - 1))) / (Math.pow(1 + mp, month) - 1);
                mi = money * mp;
                money = money - monthBM;
                allP = allP + mi;
                if (callback_m) {
                    callback_m(i, mIncome.toFixed(2), monthBM.toFixed(2), mi.toFixed(2), money.toFixed(2));
                }
                //writeCal(i, mIncome.toFixed(2), monthBM.toFixed(2), mi.toFixed(2), money.toFixed(2));
            }

            return {
                allMoney: allM,//贷款金额
                allPay: allP.toFixed(2),//利息共计
                all: (allM + allP).toFixed(2),//本息共计
                monthPay: (allP / month).toFixed(2),//平均每月偿还利息
                monthMoney: (allM / month).toFixed(2),//平均每月偿还本金,
                monthAll: (allP / month + allM / month).toFixed(2),//平均每月偿还合计
            }
        },
		/**
		 * 等额本金，计算总还款利息
		 * @param bMoney 贷款金额
		 * @param yearRatio 年利率%，如年利率5.6%就为5.6
		 * @param month 贷款月数
		 * @return mixed
		 */
        benjin: function (bMoney, yearRatio, month, callback_m) {
            var yp = yearRatio; //年利率
            var mp = yearRatio / 12; // 月利率
            //var month = t * 12; // 期限
            var allM = bMoney;
            //================================//
            var mpm = 0;
            var tmpMoney = 0;
            var mi = 0;
            var rm = allM;
            var allP = 0;
            for (var i = 1; i <= month; i++) {
                // 本息
                mpm = (allM / month) + (allM - tmpMoney) * mp;
                // 利息
                mi = (allM - tmpMoney) * mp;
                tmpMoney = tmpMoney + (allM / month);
                // 还款本金
                var baseM = (allM / month);
                rm = (rm - baseM);
                // i:期,mpm:本息,baseM:本金,mi:利息,rm:剩余本金
                //writeCal(i, mpm.toFixed(2), baseM.toFixed(2), mi.toFixed(2), rm.toFixed(2));
                if (callback_m) {
                    callback_m(i, mpm.toFixed(2), baseM.toFixed(2), mi.toFixed(2), rm.toFixed(2));
                }
                // 利息共计
                allP = allP + mi;
            }
            return {
                allMoney: allM,//贷款金额
                allPay: allP.toFixed(2),//利息共计
                all: (allM + allP).toFixed(2),//本息共计
                monthPay: (allP / month).toFixed(2),//平均每月偿还利息
                monthMoney: (allM / month).toFixed(2),//平均每月偿还本金,
                monthAll: (allP / month + allM / month).toFixed(2),//平均每月偿还合计
            }
        },
		/**
		 * 按月付息，到期还本计算器
		 * @param bMoney 贷款金额
		 * @param yearRate 年利率%，如年利率5.6%就为5.6
		 * @param month 贷款月数
		 * @return mixed
		 */
        daoqhb: function (money, yearRate, month) {
            var lixiTotal = (((money * yearRate) / 12) * month).toFixed(2);//利息总和
            var benxiTotal = Number(money) + Number(lixiTotal);//本息合计
            var repayMonth = (Number(lixiTotal) / Number(month)).toFixed(2);//每月还款

            return {
                allPay: lixiTotal,//利息总和
                all: benxiTotal,//本息合计
                monthAll: repayMonth//每月还款
            }
        },
        writeCal: function (num, allM, baseM, pM, rM) {
            var html = '<tr>';
            html += '<td>' + num + '</td>';
            html += '<td>' + allM + '</td>';
            html += '<td>' + baseM + '</td>';
            html += '<td>' + pM + '</td>';
            html += '<td>' + Math.abs(rM) + '</td>';
            html += '</tr>';
            console.log(html);
            return html;
        }
    }
    return calculator;
});